package com.blb.demo1;

/**
 * 锁对象等待
 */
public class WaitThread {

    public Object obj=new Object();

    public void testWait(){
       synchronized (obj){
           System.out.println(Thread.currentThread().getName() +"执行testWait 等待");
           try {
               obj.wait();
           } catch (InterruptedException e) {
               throw new RuntimeException(e);
           }
           System.out.println(Thread.currentThread().getName() +"结束testWait");
       }
    }

    public void testNotify(){
        synchronized (obj){
            obj.notify();
        }
    }
    public static void main(String[] args) {
        WaitThread waitThread = new WaitThread();
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                waitThread.testWait();
            }).start();
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        waitThread.testNotify();
    }
}
